<?php

namespace App\Http\Controllers\CRM\MiniAPP;

use App\Enums\MiniAPP\TableReserveStatus;
use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Resources\MiniAPP\TableReserveResource;
use App\Models\FakeTableReserve;
use Illuminate\Http\Request;

class TableReserveController extends Controller
{
    public function getList(Request $request)
    {
        $query = FakeTableReserve::latest()->with([
            'tableType',
            'member',
            'store'
        ]);

        if (!empty($request->status))
            $query = $query->where('status', $request->status);

        $reserves = $query->paginate();

        return TableReserveResource::collection($reserves);
    }

    public function reserveSuccess(FakeTableReserve $reserve)
    {
        if ($reserve->status != TableReserveStatus::Pending)
            return ApiResponseHelper::json([], 400, '预约状态不正确');

        $reserve->status = TableReserveStatus::Passed;
        $reserve->save();

        return ApiResponseHelper::json([]);
    }

    public function reserveFail(FakeTableReserve $reserve)
    {
        if ($reserve->status != TableReserveStatus::Pending)
            return ApiResponseHelper::json([], 400, '预约状态不正确');

        $reserve->status = TableReserveStatus::Rejected;
        $reserve->save();

        return ApiResponseHelper::json([]);
    }
}
